#ifndef SDLWIDGET_H
#define SDLWIDGET_H

#include <QTimer>
#include <QWidget>
#define SDL_MAIN_HANDLED
#include <SDL.h>

/* If you'd like, you can just write
 * `#define MS_PER_FRAME 17` here since that's all this is doing,
 * but I like making sure that the math is right (by not doing it myself). */
constexpr int64_t FRAME_RATE = 60;
constexpr int64_t MS_PER_FRAME = 1000 / FRAME_RATE;

class SDLWidget : public QWidget {
  public:
    SDLWidget(QWidget *parent);
    virtual ~SDLWidget() override;

    /* A custom repaint slot, I'll explain this later */
  public slots:
    void SDLRepaint();

  protected:
    SDL_Window *sdl_window;
    SDL_Renderer *sdl_renderer;

  private:
    /* To be overridden by child classes */
    virtual void Init(){};
    virtual void Update(){};
    virtual void OnResize(int w, int h){};

    void resizeEvent(QResizeEvent *event) override;
    void showEvent(QShowEvent *) override;

    /* Overriding this method prevents the
     * "QWidget::paintEngine: Should no longer be called" error. */
    QPaintEngine *paintEngine() const override;

    /* Timer used to call the re-render method every 1/60th of a second */
    QTimer frameTimer;
};

#endif // SDLWIDGET_H
